<?php

class calendarmodule {
	function name() { return exponent_lang_loadKey('modules/calendarmodule/class.php','module_name'); }
	function author() { return 'OIC Group, Inc'; }
	function description() { return exponent_lang_loadKey('modules/calendarmodule/class.php','module_description'); }

	function hasContent() { return true; }
	function hasSources() { return true; }
	function hasViews()   { return true; }

	function supportsWorkflow() { return true; }

	function getRSSContent($loc) {
				global $db;

				//Get this modules items
				$items = array();
				$items = $db->selectObjects("calendar", "location_data='".serialize($loc)."'");

				//Convert the newsitems to rss items
				$rssitems = array();
				foreach ($items as $key => $item) {
						$rss_item = new FeedItem();
						$rss_item->title = $item->title;
						$rss_item->description = $item->body;
						$rss_item->date = date('r', $item->posted);
						$rss_item->link = "http://".HOSTNAME.PATH_RELATIVE."index.php?module=calendarmodule&action=view&id=".$item->id."&src=".$loc->src;
						$rssitems[$key] = $rss_item;
				}
				return $rssitems;
		}

	function copyContent($oloc,$nloc) {

	}

	function permissions($internal = '') {
		$i18n = exponent_lang_loadFile('modules/calendarmodule/class.php');

		if ($internal == '') {
			return array(
				'administrate'=>$i18n['perm_administrate'],
				'configure'=>$i18n['perm_configure'],
				'post'=>$i18n['perm_post'],
				'edit'=>$i18n['perm_edit'],
				'delete'=>$i18n['perm_delete'],
				'approve'=>$i18n['perm_approve'],
				'manage_approval'=>$i18n['perm_manage_approval'],
				'manage_categories'=>$i18n['perm_manage_categories']
			);
		} else {
			return array(
				'administrate'=>$i18n['perm_administrate'],
				'edit'=>$i18n['perm_edit'],
				'delete'=>$i18n['perm_delete']
			);
		}
	}

	function getLocationHierarchy($loc) {
		if ($loc->int == '') return array($loc);
		else return array($loc,exponent_core_makelocation($loc->mod,$loc->src));
	}

	function show($view,$loc = null, $title = '') {
		global $user;
		global $db;

		$i18n = exponent_lang_loadFile('modules/calendarmodule/class.php');

		$locsql = "(location_data='".serialize($loc)."'";
		$config = $db->selectObject("calendarmodule_config","location_data='".serialize($loc)."'");
		if (!empty($config->aggregate)) {
			$locations = unserialize($config->aggregate);
			foreach ($locations as $source) {
				$tmploc = null;
				$tmploc->mod = 'calendarmodule';
				$tmploc->src = $source;
				$tmploc->int = '';
				$locsql .= " OR location_data='".serialize($tmploc)."'";
			}
		}
		$locsql .= ')';
		
		if (!isset($config->id))
		{
			$config->color_waiting="#C0FFC0";
			$config->color_not_approve="#FF8080";
		}
		
		$template = new template('calendarmodule',$view,$loc);
		$template->assign('moduletitle',$title);

		$canviewapproval = false;
		$inapproval = false;

		global $user;
		if ($user) $canviewapproval = (exponent_permissions_check("approve",$loc) || exponent_permissions_check("manage_approval",$loc));
		if ($db->countObjects("calendar","location_data='".serialize($loc)."' AND approve!=1")) 
			$inapproval = true;

		$time = (isset($_GET['time']) ? $_GET['time'] : aboid_getCurrentTime());
		//pre calculate first day of this week, and last day of this week
		// coi như ngày đầu tuần là ngày CN
		$first_day=calendarmodule::FirstDay_inWeek($time);
		$last_day=calendarmodule::LastDay_inWeek($time);
		$begin_ofday=calendarmodule::DateClearTime($time);
		
		$template->assign("time",$time);
		$template->assign("begin_of_today",$begin_ofday);
		$template->assign("wfirst_day",$first_day);
		$template->assign("wlast_day",$last_day);
		
		$template->assign('in_approval',$inapproval);
		$template->assign('canview_approval_link',$canviewapproval);
		$template->register_permissions(
			array('administrate','configure','post','edit','delete','manage_approval','manage_categories'),
			$loc
		);


		$cats = $db->selectObjectsIndexedArray("category","location_data='".serialize($loc)."'");
		$cats[0] = null;
		$cats[0]->name = '<i>'.$i18n['no_category'].'</i>';
		$cats[0]->color = "#000000";
		$template->assign("categories",$cats);

		if (!$config) {
			$config->enable_categories = 0;
		}

		$template->assign("modconfig",$config);
		$template->output();
	}

	function deleteIn($loc) {
		global $db;
		$refcount = $db->selectValue('sectionref', 'refcount', "source='".$loc->src."'");
				if ($refcount <= 0) {
			$items = $db->selectObjects("calendar","location_data='".serialize($loc)."'");
			foreach ($items as $i) {
				$db->delete("calendar_wf_revision","wf_original=".$i->id);
				$db->delete("calendar_wf_info","real_id=".$i->id);
			}
			$db->delete("calendar","location_data='".serialize($loc)."'");
		}
	}
	// 10/10/2009: immanuel192 disabled search function
	function searchName_disable() {
				return "Calendar Events";
		}

	function spiderContent_diable($item = null) {
		global $db;

		$i18n = exponent_lang_loadFile('modules/calendarmodule/class.php');

		if (!defined('SYS_SEARCH')) include_once(BASE.'subsystems/search.php');

		$search = null;
		$search->category = $i18n['search_category'];
		$search->view_link = ''; // FIXME : need a view action
		$search->ref_module = 'calendarmodule';
		$search->ref_type = 'calendar';

		if ($item) {
			$db->delete('search',"ref_module='calendarmodule' AND ref_type='calendar' AND original_id=" . $item->id);
			$search->original_id = $item->id;
			$search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
			$search->title = ' ' . $item->title . ' ';
			$search->location_data = $item->location_data;
			$db->insertObject($search,'search');
		} else {
			$db->delete('search',"ref_module='calendarmodule' AND ref_type='calendar'");
			foreach ($db->selectObjects('calendar') as $item) {
				$search->original_id = $item->id;
				$search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
				$search->title = ' ' . $item->title . ' ';
				$search->location_data = $item->location_data;
				$db->insertObject($search,'search');
			}
		}

		return true;
	}

	static function FirstDay_inWeek($date){
		$first_day=$date;
		if (date("D", $first_day)!="Mon")
			$first_day=strtotime("last Monday", $first_day);
		$first_day=calendarmodule::DateClearTime($first_day);
		return $first_day;
	}
	
	static function LastDay_inWeek($date){
		// 
		$last_day=$date;
		if (date("D", $last_day)!="Sun")
			$last_day=strtotime("next Sunday", $last_day);
		$last_day=calendarmodule::DateClearTime($last_day)+24*3600-1;
		return $last_day;
	}
	
	static function DateClearTime(&$date){
		$date=mktime(0, 0,0,date("n", $date),date("j", $date),date("Y", $date));
		return $date;
	}
}
?>
